<?php
/**
 * Created by PhpStorm.
 * User: dx
 * Date: 17/06/14
 * Time: 下午3:13
 */
class CommonController extends BaseController {
    /**
     * @Author:      dx
     * @DateTime:    2017-06-14 16:35:29
     * @Description: C app端系统的升级
     */
    public function app_update(){
        $validator = Validator::make(Input::all(),
            array(
                'version' => 'required',
            )
        );
    
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.args_error_1000");
                break;
            }
            $ou=CommonAppVersions::orderBy('created_at','desc')->first();
            if($ou){
                $url=Config::get("config.apk_url").$ou->url;
                $result=array("is_upgrade"=>$ou->force_update,"version"=>$ou->version,"url"=>$url);
            }else{
                $result=array("is_upgrade"=>0,"version"=>1,"url"=>'');
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "wechat2.0" ,"event" => "app_update", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      dx
     * @DateTime:    2016-12-06 13:33:01
     * @Description: 图片上传
     */
    public function uploadimage(){
        error_reporting(E_ERROR);
//            header("Content-Type: text/html; charset=utf-8");
//            header('Access-Control-Allow-Origin: *'); //设置http://www.baidu.com允许跨域访问
//            header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With'); //设置允许的跨域header
        $action = Input::get('action');
        /* 上传配置 */
        $base64 = "";
        $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("../app/config/config.json")), true);
        $result = [];
        switch ($action) {
            case 'config':
                $result =  json_encode($CONFIG);
                /* 输出结果 */
                if (isset($_GET["callback"])) {
                    if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
                        $result = htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
                    } else {
                        $result =(array(
                            'state'=> 'callback参数不合法'
                        ));
                    }
                }
                break;
            /* 上传图片 */
            case 'uploadimage':
                $config = array(
                    "pathFormat" => $CONFIG['imagePathFormat'],
                    "maxSize" => $CONFIG['imageMaxSize'],
                    "allowFiles" => $CONFIG['imageAllowFiles'],
                    'pic_img_file' => Config::get('config.pic_img_file')
                );
                $fieldName = $CONFIG['imageFieldName'];
                /* 生成上传实例对象并完成上传 */
                $up = new Uploader($fieldName, $config, $base64);
                $result = (($up->getFileInfo()));
                break;
            default:
                $result = (array(
                    'state'=> '请求地址出错'
                ));
                break;
        }
        $response = Response::make($result, 200);

        $response->header('Access-Control-Allow-Origin', '*');
        $response->header('Access-Control-Allow-Headers', 'X-Requested-With,X_Requested_With');
        $response->header('Access-Control-Allow-Headers', 'X-Requested-With, X_Requested_With');
        //$response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, Accept');
        $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
        $response->header('Access-Control-Allow-Credentials', 'true');
        return $response;
    }

    /*
   * @Author:      dx
   * @DateTime:    2017-06-28 11:51:11
   * @Description: 协议 1-C端 2-APP
   */
    public function network_protocol(){
        $validator = Validator::make(Input::all(),
            array(
                'source' => 'required',
                'type' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok','result'=>array());
        do{
            if ($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $res = AhpArticle::where('status',1)->where('type',Input::get('type'))->where('source',Input::get('source'))->orderBy('id','desc')->first();
            $result = array(
                'content' => $res
            );
            $response = array( 'code' => 0,'msg' => 'ok','result' =>$result );
        }while(false);
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:    2017-08-16 15:32:01
     * @Description: app下载
     */
    public function app_download(){
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $type = 'other';
        if(strpos($agent, 'iphone') || strpos($agent, 'ipad'))
        {
            $type = 'https://itunes.apple.com/cn/app/%E5%AE%89%E6%83%A0%E5%85%BB%E8%BD%A6/id1251290884?mt=8';
        }
        if(strpos($agent, 'android'))
        {
            if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
                $type = "http://app.qq.com/#id=detail&appid=1106237186";
            }else{
                $android_app = CommonAppVersions::orderBy('created_at','desc')->first();
                $type = Config::get('config.apk_url').$android_app->url;
            }
        }
        if($type == 'other'){
            echo "<script language=javascript>alert('不支持该设备进行下载');history.back();</script>";
            exit;
        }
        header('location:'. $type);
    }
}